import 'dart:ui';

import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/config/lf_route.dart';
import 'package:largefriends/config/lf_type_config.dart';
import 'package:largefriends/service/manager/lf_store_helper.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/lf_theme.dart';
import 'package:largefriends/res/lf_widget_ext.dart';
import 'package:largefriends/res/strings/lf_strings_i18n.dart';
import 'package:largefriends/service/im/lf_im_service.dart';
import 'package:largefriends/service/lf_auth_service.dart';
import 'package:largefriends/utils/lf_keyboard.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  await LfStorageManager.init();
  Get.put(LfAuthService());
  Get.put(LfIMService());
  await ScreenUtil.ensureScreenSize();
  TypeConfig().initConfig();
  EasyRefresh.defaultHeaderBuilder =
      () => CupertinoHeader(foregroundColor: colorFF007F);
  EasyRefresh.defaultFooterBuilder =
      () => CupertinoFooter(foregroundColor: colorFF007F, emptyWidget: empty);
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      getPages: LFRoute.getPages,
      debugShowCheckedModeBanner: false,
      translations: LfStringsI18n(),
      fallbackLocale: const Locale('en', 'US'),
      locale: window.locale,
      initialRoute: LFRoute.pathSplash,
      builder: (context, child) {
        ScreenUtil.init(
          context,
          designSize: const Size(375, 677),
          minTextAdapt: true,
          splitScreenMode: true,
        );
        return MediaQuery(
          data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
          child: Scaffold(
            body: GestureDetector(
              onTap: () {
                closeSoftKeyboard();
              },
              child: child,
            ),
          ),
        );
      },
      theme: lightTheme,
      darkTheme: lightTheme,
      themeMode: ThemeMode.light,
    );
  }
}
